home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gcore / RCS / process.c,v < prev   
Encoding:
Text File  |  1991-10-25  |  12.1 KB  |  518 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     91.10.25.10.28.36;  author jhh;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     89.02.22.11.35.59;  author jhh;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     89.02.05.21.11.32;  author mendel;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     89.02.05.20.51.10;  author mendel;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @The gcore program.
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @now works on sun4's
  38. @
  39. text
  40. @/* 
  41.  * process.c --
  42.  *
  43.  *    Routines for finding and manipulating processes for the
  44.  *    gcore program.
  45.  *
  46.  * Copyright 1988 Regents of the University of California
  47.  * Permission to use, copy, modify, and distribute this
  48.  * software and its documentation for any purpose and without
  49.  * fee is hereby granted, provided that the above copyright
  50.  * notice appear in all copies.  The University of California
  51.  * makes no representations about the suitability of this
  52.  * software for any purpose.  It is provided "as is" without
  53.  * express or implied warranty.
  54.  */
  55.  
  56. #ifndef lint
  57. static char rcsid[] = "$Header: /a/newcmds/gcore/RCS/process.c,v 1.3 89/02/22 11:35:59 jhh Exp $ SPRITE (Berkeley)";
  58. #endif not lint
  59.  
  60.  
  61. #include <ctype.h>
  62. #include <option.h>
  63. #include <status.h>
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <string.h>
  67. #include <signal.h>
  68. #include <sys/types.h>
  69. #include <sys/file.h>
  70. #include <proc.h>
  71. #include <vm.h>
  72.  
  73. #include "gcore.h"
  74.  
  75.  
  76. /*
  77.  *----------------------------------------------------------------------
  78.  *
  79.  *  FindProcess --
  80.  *
  81.  *      Find the process and argument string of the specified process.
  82.  *
  83.  * Results:
  84.  *    The process state.
  85.  *
  86.  *
  87.  * Side effects:
  88.  *      The none.
  89.  *
  90.  *----------------------------------------------------------------------
  91.  */
  92.  
  93. int
  94. FindProcess(pid,argString,segSizePtr,sigStatePtr)
  95.     int        pid;        /* Process ID to locate. */
  96.     char    *argString;    /* Area to place argument string of
  97.                  * the specified process. */
  98.     int        *segSizePtr;    /* Array to store segment size in. */
  99.     int        *sigStatePtr;   /* Out: State of the signal masks. 
  100.                  * In: The signal to check. */
  101. {
  102.     int  pcbsUsed;
  103.     ReturnStatus status;
  104.     Proc_PCBInfo    pcb;
  105.     Proc_PCBInfo    *pcbPtr;
  106.     Proc_PCBArgString    pcbArgString;
  107.     int            procState;
  108.  
  109.     /*
  110.      * Find the PCB of the specifed process.
  111.      */
  112.     procState = NOT_FOUND_STATE;
  113.     pcbPtr = &pcb;
  114.     status = Proc_GetPCBInfo(Proc_PIDToIndex(pid),
  115.                  Proc_PIDToIndex(pid), PROC_MY_HOSTID,
  116.                  sizeof(Proc_PCBInfo),
  117.                  pcbPtr, &pcbArgString, &pcbsUsed);
  118.     if (status != SUCCESS) {
  119.     if (debug) {
  120.         (void) fprintf(stderr, "Proc_GetPCBInfo for pid 0x%x error: %s",pid,
  121.             Stat_GetMsg(status));
  122.     }
  123.     return procState;
  124.     }
  125.  
  126.     /*
  127.      * The process we got info for may not be the one that was
  128.      * requested (different generation numbers);  check to be sure.
  129.      */
  130.     if (pid != pcbPtr->processID) {
  131.     if (debug) {
  132.         (void) fprintf(stderr,
  133.             "Proc_GetPCBInfo: looking for 0x%x but found 0x%x\n",
  134.            pid,pcbPtr->processID);    
  135.         }
  136.         return procState;
  137.     }
  138.     /*
  139.      * Check the process state.
  140.      */
  141.     switch (pcbPtr->state) {
  142.     case PROC_SUSPENDED: {
  143.     procState = (pcbPtr->genFlags & (PROC_DEBUGGED | PROC_ON_DEBUG_LIST)) ?
  144.                 DEBUG_STATE : 
  145.                 SUSPEND_STATE;
  146.     break;
  147.         }
  148.     case PROC_RUNNING:
  149.     case PROC_READY:
  150.     case PROC_WAITING: {
  151.     procState = RUN_STATE;
  152.     break;
  153.         }
  154.     default:
  155.     procState = UNKNOWN_STATE;
  156.     }
  157.  
  158.     /*
  159.      * Copy the argument string to return it.
  160.      */
  161.     if (argString != NULL) {
  162.     char    *ap = pcbArgString.argString;
  163.     (void) strncpy(argString,ap,MAX_ARG_STRING_SIZE-1);
  164.     argString[MAX_ARG_STRING_SIZE-1] = 0;
  165.     if (debug) {
  166.         (void) fprintf(stderr,"FindProcess: Argument string \"%s\"\n",ap);
  167.     }
  168.    }
  169.    /*
  170.     * Lookup the segment size from the virtual memory system.
  171.     */
  172.     if (segSizePtr != (int *) 0) {
  173.       Vm_SegmentInfo     segBuf[VM_NUM_SEGMENTS];
  174.       int                 pagesize = getpagesize();
  175.  
  176.       status = Vm_GetSegInfo(pcbPtr, 0, sizeof(Vm_SegmentInfo),
  177.                      &(segBuf[1]));
  178.       if (status != SUCCESS) {
  179.         if(debug) {
  180.         (void) fprintf(stderr, 
  181.             "Couldn't read segment info for pid %x: %s\n",
  182.             pcbPtr->processID, Stat_GetMsg(status));
  183.         }
  184.       }
  185.       segSizePtr[TEXT_SEG] = segBuf[VM_CODE].numPages*pagesize;
  186.       segSizePtr[DATA_SEG] = segBuf[VM_HEAP].numPages*pagesize;
  187.       segSizePtr[STACK_SEG] = segBuf[VM_STACK].numPages*pagesize;
  188.     }
  189.  
  190.    if (sigStatePtr != (int *) 0) {
  191.     int    spriteSig;
  192.     (void) Compat_UnixSignalToSprite(*sigStatePtr,&spriteSig);
  193.     if (pcbPtr->sigActions[spriteSig] == SIG_IGNORE_ACTION) {
  194.         *sigStatePtr = SIG_IGNORING;
  195.     } else if (pcbPtr->sigActions[spriteSig] > SIG_NUM_ACTIONS) {
  196.         *sigStatePtr = SIG_HANDLING;
  197.     } else if (pcbPtr->sigHoldMask & spriteSig) {
  198.         *sigStatePtr = SIG_HOLDING;
  199.     } else {
  200.         *sigStatePtr = 0;
  201.     }
  202.  
  203.    }
  204.    return (procState);
  205.  
  206. }
  207.  
  208. /*
  209.  *----------------------------------------------------------------------
  210.  *
  211.  *  XferSegmentFromProcess --
  212.  *
  213.  *      Transfer a memory segment from a process into a file. 
  214.  *
  215.  *     This routines transfers the memory image from the process 
  216.  *    specified by pid into the file coreFile. The transfer starts
  217.  *    at the address specified by startAddress and ends on the first
  218.  *    unreadable page. 
  219.  *
  220.  * Results:
  221.  *    Number of bytes written to file. -1 if an error occured.
  222.  *
  223.  * Side effects:
  224.  *      The file is written.
  225.  *
  226.  *----------------------------------------------------------------------
  227.  */
  228.  
  229. int
  230. XferSegmentFromProcess(pid,startAddress,coreFile)
  231.     int          pid;        /* Process ID to operate on. */
  232.     unsigned int startAddress;    /* Address to start with. */
  233.     FILE    *coreFile;    /* File to write image to. */
  234. {
  235.     ReturnStatus                status = SUCCESS;
  236.     int                xferSize = getpagesize();
  237.     char            *xferBuffer;
  238.     unsigned int        nextAddress;
  239.  
  240.     /*
  241.      * Round the starting address to point at the start of its page.
  242.      */
  243.  
  244.     startAddress = startAddress & ~(xferSize-1);
  245.     nextAddress = startAddress;
  246.     /*
  247.      * Allocate the buffer to copy.
  248.      */
  249.     xferBuffer = malloc(xferSize);
  250.     /*
  251.      * Read from memory and write to file until we get an error.
  252.      */
  253.     status = Proc_Debug((Proc_PID)pid,PROC_READ, xferSize, 
  254.                 (char *)nextAddress,xferBuffer);
  255.     while (status == SUCCESS) {
  256.     if (fwrite(xferBuffer,xferSize,1,coreFile) != 1) {
  257.         perror(PROGRAM_NAME);
  258.         return (-1);
  259.     }
  260.     nextAddress += xferSize;
  261.     status = Proc_Debug((Proc_PID) pid,PROC_READ,xferSize,
  262.                 (char *)nextAddress,xferBuffer);
  263.     }
  264.  
  265.     if (debug) {
  266.     (void) fprintf(stderr,"XferSegment 0x%x - 0x%x from 0x%x\n",
  267.             startAddress,nextAddress, pid);
  268.     }
  269.  
  270.     /*
  271.      * Return the number of bytes transfered.
  272.      */
  273.     return (nextAddress-startAddress);
  274. }
  275.  
  276.  
  277. /*
  278.  *----------------------------------------------------------------------
  279.  *
  280.  *  ReadStopInfoFromProcess --
  281.  *
  282.  *      Read the stop info (ie registers and fault code) from
  283.  *    the specified process.
  284.  *
  285.  * Results:
  286.  *    True if operation succeeded false otherwise.
  287.  *
  288.  * Side effects:
  289.  *      The process is "attached" using the Proc_Debug call.
  290.  *
  291.  *----------------------------------------------------------------------
  292.  */
  293.  
  294.  
  295. Boolean
  296. ReadStopInfoFromProcess(pid,signalNumPtr,regsPtr)
  297.     int        pid;        /* Process ID to read. */
  298.     int        *signalNumPtr;    /* Signal number of fault. */
  299.     struct    regs *regsPtr;  /* Regs of process.     */
  300. {
  301.     ReturnStatus    status;
  302.     Proc_DebugState process_state;
  303.  
  304.     /*
  305.      * Attach the process.
  306.      */
  307.     status = Proc_Debug((Proc_PID)pid,PROC_GET_THIS_DEBUG,0,(char *)0,
  308.                         (char *)0);
  309.     if (status != SUCCESS) {
  310.     (void) fprintf(stderr, "%s: Can't attach process 0x%x error: %s", 
  311.             PROGRAM_NAME, pid, Stat_GetMsg(status));
  312.  
  313.     return FALSE;
  314.     }
  315.     /*
  316.      * Read the process's stop state.
  317.      */
  318.     status = Proc_Debug((Proc_PID)pid, PROC_GET_DBG_STATE, 0,
  319.                     (char *)0,(char *)&process_state);
  320.     if (status != SUCCESS) {
  321.     (void) fprintf(stderr, "%s: Read state of process 0x%x error: %s", 
  322.             PROGRAM_NAME, pid, Stat_GetMsg(status));
  323.  
  324.     return FALSE;
  325.     }
  326.     /*
  327.      * Map the Sprite signal to a Unix style signal using a routine 
  328.      * in libc.a.
  329.      */
  330.     (void)Compat_SpriteSignalToUnix(process_state.termStatus,signalNumPtr);
  331.     /*
  332.      * Convert the Mach_RegState structure return by Proc_Debug into a
  333.      * Unix "struct regs".
  334.      */
  335.     ConvertSpriteRegsToUnixRegs(&process_state.regState,regsPtr);
  336.     return (TRUE);
  337. }
  338.  
  339. Boolean
  340. AttachProcess(pid)
  341. {
  342.     ReturnStatus    status;
  343.     /*
  344.      * Attach the process.
  345.      */
  346.     status = Proc_Debug((Proc_PID)pid,PROC_GET_THIS_DEBUG,0,(char *)0,
  347.                         (char *)0);
  348.     if (status != SUCCESS) {
  349.     (void) fprintf(stderr, 
  350.             "%s: Error attaching process %x: %s\n", PROGRAM_NAME,
  351.             pid, Stat_GetMsg(status));
  352.  
  353.     return FALSE;
  354.     }
  355.     return TRUE;
  356. }
  357.  
  358. Boolean
  359. DetachProcess(pid)
  360. {
  361.     ReturnStatus    status;
  362.     /*
  363.      * Attach the process.
  364.      */
  365.     status = Proc_Debug((Proc_PID)pid,PROC_DETACH_DEBUGGER,0,(char *)0,
  366.                                  (char *)0);
  367.     if (status != SUCCESS) {
  368.     if (status != PROC_INVALID_PID) { 
  369.         (void) fprintf(stderr, 
  370.             "%s: Error detaching process %x: %s\n", PROGRAM_NAME,
  371.             pid, Stat_GetMsg(status));
  372.     }
  373.     return FALSE;
  374.     }
  375.     return TRUE;
  376. }
  377.  
  378. ConvertSpriteRegsToUnixRegs(spriteRegsPtr,regsPtr)
  379. Mach_RegState    *spriteRegsPtr;
  380. struct    regs    *regsPtr;
  381. {
  382.     /* Mach_RegState defined in kernel/machTypes.h */
  383.     bcopy((char *)spriteRegsPtr->ins,(char *)&(regsPtr->r_o0),
  384.       8*sizeof(unsigned int));
  385.     regsPtr->r_y = spriteRegsPtr->y;
  386.     bcopy((char *)spriteRegsPtr->globals,(char *)&(regsPtr->r_g1),
  387.       7*sizeof(unsigned int));
  388.     /*assume r_sr changed to r_psr in struct regs and statusReg */
  389.     /*changed to curPsr */
  390.     regsPtr->r_psr = spriteRegsPtr->curPsr;
  391.     regsPtr->r_pc = spriteRegsPtr->pc;
  392. }
  393. @
  394.  
  395.  
  396. 1.3
  397. log
  398. @Now uses new Proc_GetPCBInfo and Vm_GetSegInfo interfaces
  399. @
  400. text
  401. @d18 1
  402. a18 1
  403. static char rcsid[] = "$Header: /a/newcmds/gcore/RCS/process.c,v 1.2 89/02/05 21:11:32 mendel Exp $ SPRITE (Berkeley)";
  404. d21 1
  405. d210 1
  406. a210 1
  407.     xferBuffer = alloca(xferSize);
  408. d340 2
  409. a341 2
  410.     Mach_RegState    *spriteRegsPtr;
  411.     struct    regs    *regsPtr;
  412. d343 9
  413. a351 2
  414.     bcopy((char *)spriteRegsPtr->regs,(char *)regsPtr,16*4);
  415.     regsPtr->r_sr = spriteRegsPtr->statusReg;
  416. a353 2
  417.  
  418.  
  419. @
  420.  
  421.  
  422. 1.2
  423. log
  424. @Cleanup for lint.
  425. @
  426. text
  427. @d18 1
  428. a18 1
  429. static char rcsid[] = "$Header: /a/newcmds/gcore/RCS/process.c,v 1.1 89/02/05 20:51:10 mendel Exp Locker: mendel $ SPRITE (Berkeley)";
  430. d30 1
  431. a30 1
  432. #include <kernel/proc.h>
  433. a31 1
  434. #include <kernel/vm.h>
  435. d64 2
  436. a65 2
  437.     Proc_ControlBlock    pcb;
  438.     Proc_ControlBlock    *pcbPtr;
  439. d76 1
  440. d133 2
  441. a134 2
  442.       Vm_Segment segBuf[VM_NUM_SEGMENTS];
  443.       int         pagesize = getpagesize();
  444. d136 2
  445. a137 1
  446.       status = Vm_GetSegInfo(pcbPtr, 0, &(segBuf[1]));
  447. @
  448.  
  449.  
  450. 1.1
  451. log
  452. @Initial revision
  453. @
  454. text
  455. @d18 1
  456. a18 1
  457. static char rcsid[] = "$Header: /a/newcmds/ps/RCS/ps.c,v 1.13 88/12/22 11:04:48 douglis Exp $ SPRITE (Berkeley)";
  458. d151 1
  459. a151 1
  460.     Compat_UnixSignalToSprite(*sigStatePtr,&spriteSig);
  461. a164 55
  462. }
  463.  
  464. /*
  465.  *----------------------------------------------------------------------
  466.  *
  467.  *  GetSigMask --
  468.  *
  469.  *      Find the process the sigmasks of a process.
  470.  *
  471.  * Results:
  472.  *    None.
  473.  *
  474.  *
  475.  * Side effects:
  476.  *      The none.
  477.  *
  478.  *----------------------------------------------------------------------
  479.  */
  480.  
  481. void
  482. GetSigMask (pid, heldMaskPtr, ignoredMaskPtr, handleMaskPtr)
  483.     int        pid;        /* Process ID. */
  484.     int        *heldMaskPtr;    /* Held mask of process. */
  485.     int        *ignoredMaskPtr; /* Ignored mask of process. */
  486.     int        *handleMaskPtr; /* Handle mask of process. */
  487. {
  488.     int  pcbsUsed;
  489.     ReturnStatus status;
  490.     Proc_ControlBlock    pcb;
  491.     Proc_ControlBlock    *pcbPtr;
  492.     Proc_PCBArgString    pcbArgString;
  493.  
  494.     *heldMaskPtr = *ignoredMaskPtr = *handleMaskPtr = 0;
  495.  
  496.     /*
  497.      * Find the PCB of the specifed process.
  498.      */
  499.     pcbPtr = &pcb;
  500.     status = Proc_GetPCBInfo(Proc_PIDToIndex(pid),
  501.                  Proc_PIDToIndex(pid), PROC_MY_HOSTID,
  502.                  pcbPtr, &pcbArgString, &pcbsUsed);
  503.     if (status == SUCCESS) {
  504.     int    ignore, handle, i;
  505.     ignore = handle = 0;
  506.     for (i = 1; i <= SIG_NUM_SIGNALS; i++) {
  507.         if (pcbPtr->sigActions[i] == SIG_IGNORE_ACTION) {
  508.         ignore |= 1<<(i-1);
  509.         } else if (pcbPtr->sigActions[i] >> SIG_NUM_ACTIONS) {
  510.         handle |= 1<<(i-1);
  511.         }
  512.     }
  513.     Compat_SpriteSigMaskToUnix(pcbPtr->sigHoldMask,heldMaskPtr);
  514.     Compat_SpriteSigMaskToUnix(ignore,ignoredMaskPtr);
  515.     Compat_SpriteSigMaskToUnix(handle,handleMaskPtr);
  516.     }
  517. @
  518.